﻿@model IEnumerable<EdexApiClient.WH_COMPANIES>

@{
    ViewBag.Title = "Справочник контрагентов";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<header class="jumbotron subhead" id="overview">
    <h1></h1>
    <p class="lead">Справочник контрагентов</p>
</header>

<form>
    <div class="input-append">
        <input type="text" class="input-xxlarge" name="search" value="@Request["search"]" />
        <button class="btn" type="submit"><i class="icon-filter"></i> Фильтр</button>
    </div>
</form>

<table data-name="WH_COMPANIES" class="table table-striped">
    <thead>
        <tr>
            <th>Код</th>
            <th>Наименование</th>
            <th>Связанная компания</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var record in Model) {
        <tr data-index="@record.GUID_RECORD">
            <td>@record.EXT_KEY</td>
            <td>@record.COMPANY_NAME</td>
            <td>@(record.MAP.RIGHT_COMPANY_MAP != null  
                    ? record.MAP.RIGHT_COMPANY_MAP.COMPANY_NAME 
                    : String.Empty)</td>
        </tr>
        }
    </tbody>
</table>

<p>
    <a href="#" class="btn" data-action="COMPANY-CREATE"><i class="icon-plus"></i> Создать нового контрагента</a>
</p>

<div id="company-details"></div>


<script type="text/javascript">

    $(function () {

        $("table[data-name='WH_COMPANIES'] tr td[data-type!='noclick']").live("click", function () {

            var id = $(this).parent().attr("data-index");

            $("#company-details").load('/supplier/details/' + id, function () {
                $('.modal[data-name="WH_COMPANIES"]').modal("show");
            });
        });

        $(".btn[data-action='COMPANY-CREATE']").live("click", function () {

            $("#company-details").load('/supplier/detailsnew/', function () {
                $('.modal[data-name="WH_COMPANIES"]').modal("show");
            });

        });

        $(".btn[data-action='COMPANY-DELETE']").live("click", function () {

            var id = $("form[name='WH_COMPANIES'] *[name='GUID_RECORD']").val();

            $.ajax({
                type: 'POST',
                url: '/supplier/delete/' + id,
                success: function () {
                    $('.modal[data-name="WH_COMPANIES"]').modal("hide");
                }
            });

        });

        $(".btn[data-action='COMPANY-SAVE']").live("click", function () {

            $.ajax({
                type: 'POST',
                url: '/supplier/save/',
                data: $("form[name='WH_COMPANIES']").serialize(),
                success: function () {
                    $('.modal[data-name="WH_COMPANIES"]').modal("hide");
                }
            });

        });

        $(".btn[data-action='COMPANY-FILTER']").live("click", function () {

            var filter = $("input[name='COMPANY-FILTER']").val();
            $("select[name='COMPANY_GUID-FILTER']").load("/company/FindAndReturnHtmlList/" + filter);
        });

        $(".btn[data-action='COMPANY-FILTER-APPLY']").live("click", function () {
            var COMPANY_GUID = $("select[name='COMPANY_GUID-FILTER'] option:selected").val();
            var SUPPLIER_GUID = $("input[name='GUID_RECORD']").val();

            $.ajax({
                type: 'POST',
                url: '/supplier/MapSave/',
                data: {
                    COMPANY_GUID: COMPANY_GUID,
                    SUPPLIER_GUID: SUPPLIER_GUID
                },
                success: function () {
                    alert("Соответствие проставлено");
                }
            });
        });

    });

</script>